<?php

/**
 * 购物车接口
 * 用于Ajax数据调用
 * 
 * 购物车格式:
 *      数组格式：array(array('pid'=>1,'num'=>1,'color'=>'red','remark'=>''),array('pid'=>1,'num'=>1,'color'=>'red','remark'=>''));
 *      字串格式：[{'pid':1,'num':1,'color':'red','remark':''},{'pid':1,'num':1,'color':'red','remark':''}]
 * 
 * 使用方法：
 * ?m=Api/ShoppingCar&action=getcount // 获取购物车商品数量
 * ?m=Api/ShoppingCar&action=getamount // 获取购物车商品金额
 * ?m=Api/ShoppingCar&action=update&pid=1&num=1.5&color=orange&remark=颜色要橘色的 // 更新购物车数据
 * ?m=Api/ShoppingCar&action=remove&pid=1 // 删除购物车数据
 * 
 * @author Alen
 * @since 1.0.0 2013-4-26 sutroon, 2013-10-16 sutroon 重构
 */
class ShoppingCarAction extends SoAction {

    //put your code here

    public function __construct() {
        parent::__construct(false);
    }
    public function index(){
        
    }

    /**
     * 获取购物车商品数量
     * @since 1.0.0 2013-10-16 sutroon
     */
    public function getcount() {
        $str_car = request_string('car', '', 'COOKIE');
        if (!$str_car){
            echo 0;
            exit();
        }
        $arr_car = json_decode($str_car);
        if (!is_array($arr_car)){
            echo 0;
            exit();
        }
        $a = 0;
        foreach ($arr_car as $o_car) {
            $a+=$o_car->num;
        }
        echo $a;
        exit();
    }

    /**
     * 获取购物车商品总额
     * @since 1.0.0 2013-10-16 sutroon
     */
    public function getamount() {
        $str_car = request_string('car', '', 'COOKIE');
        if (!$str_car){
            echo 0;
            exit();
        }
        $arr_car = json_decode($str_car);
        if (!is_array($arr_car)){
            echo 0;
            exit();
        }
        $a = '0';
        foreach ($arr_car as $o_car) {
            $a.=',' . $o_car->pid;
        }
        $dm = new SoDataModule();
        $rs = $dm->select_arrayset('tproductgoods', 'ID in (' . $a . ')', 'Price');
        if (!$rs){
            echo 0;
            exit();
        }
        $num = 0;
        foreach ($rs as $item) {
            $num+=$item['Price'];
        }
        echo $num;
    }

    /**
     * 更新购物车内容
     * @since 1.0.0 2013-10-16 sutroon
     */
    public function update() {
        $pid = request_numeric('pid');
        $num = request_numeric('num');
        if (!$pid || !$num){
            echo 'Invalid Argument!';
            exit();
        }

        $str_car = request_string('car', '', 'COOKIE');
        if (!$str_car) {
            // 购物车是空的,则直接设置购物车数据并退出
            $_COOKIE['car'] = '[{"pid":' . $pid . ',"num":' . $num . '}]';
            echo 'SUCCESS';
            exit();
        }
        $arr_car = json_decode($str_car);
        if (!is_array($arr_car)) {
            // 购物车是空的,则直接设置购物车数据并退出
            $_COOKIE['car'] = '[{"pid":' . $pid . ',"num":' . $num . '}]';
            echo 'SUCCESS';
            exit();
        }
        foreach ($arr_car as $o_car) {
            if ($o_car->pid == $pid) {
                $o_car->num = $num;
                break;
            }
        }
        $_COOKIE['car']=  json_encode($arr_car);
        echo 'SUCCESS';
            exit();
    }

    public function remove() {
        $pid = request_numeric('pid');
        if (!$pid){
            echo 'Invalid Argument!';
            exit();
        }

        $str_car = request_string('car', '', 'COOKIE');
        if (!$str_car){
            echo 'SUCCESS'; // 购物车是空的,直接返回成功提示
            exit();
        }

        $arr_car = json_decode($str_car);
        if (!is_array($arr_car)){
            echo 'SUCCESS'; // 购物车是空的,直接返回成功提示
            exit();
        }
        foreach ($arr_car as $o_car) {
            if ($o_car->pid == $pid) {
                $o_car->num= $num;
                break;
            }
        }
        $_COOKIE['car']=  json_encode($arr_car);
        echo 'SUCCESS';
        exit();
    }
}

?>
